import datetime

from django.db import models

from sportsground.utils.baseModels import BaseModels


class Drawback(BaseModels):
    class Meta():
        db_table = 'drawback'
        verbose_name = '退款'
        verbose_name_plural = verbose_name

    bid = models.CharField(max_length=20, primary_key=True, verbose_name="退款编号")
    price = models.FloatField(verbose_name="退款金额")
    message = models.CharField(max_length=200, verbose_name="理由")

    def Info(self):
        return {
            "bid": self.bid,
            "price": self.price,
            "message": self.message,
            "createTime": self.createTime,
        }
    @classmethod
    def getBid(cls):
        today = datetime.date.today()
        bidL = f"{today.year}{today.month:02}{today.day:02}"
        drawback = cls.objects.last()
        if not drawback:  # 第一个
            bid = bidL + "0001"
        else:
            if drawback.bid[:8] == bidL:  # 当日有订单
                bid = f"{int(drawback.bid) + 1}"
            else:  # 当日首订单
                bid = bidL + "0001"
        return bid

    @classmethod
    def create(cls, user, price, message):
        bid = cls.getBid()
        drawback = cls(bid=bid, price=price, message=message)
        drawback.save()
        user.income(price)
        return
